<!DOCTYPE html>
<html style="height: 100%">
<head>
    <title>文档首页</title>
    <% include ../../layouts/import.ejs %>
</head>
<body style="height: 100%">
<% include ../../layouts/header.ejs %>
<div class="index container" style="background: white;">
    <div class="row" style="margin:80px 0;">
        <div class="col-md-3">
            <a href="javascript:void(0);" type="button" class="btn btn-primary" id="moduleAdd">添加分类</a>
            <ul id="wordTypeTree" class="ztree"></ul>
        </div>
        <div class="col-md-9">
            <div style="margin:20px 0;">
                <!--<a href="/daily/word/add" class="btn btn-success">-->
                <a href="javascript:void(0);" id="addWord" class="btn btn-success">
                    新建文档
                </a>
            </div>
            <div id="wordList">

            </div>
        </div>
    </div>
</div>


</body>

<script>
    $(function () {
        getWordDataGrid();
        var setting = {
            async: {
                enable: true,
                url: "/daily/wordType/wordTree",
                autoParam: ["id=parentId"]
            },
            edit: {
                enable: true
            },
            callback: {
                onClick: function (event, treeId, treeNode, clickFlag) {
                    if (treeNode && treeNode.id) {
                        getWordDataGrid(treeNode.code);
                    }
                },
                beforeRemove: function (treeId, treeNode) {
                    if (treeNode.isParent) {
                        alert('当前模块有子模块，不能删除');
                        return false;
                    } else {
                        wordTypeTree.selectNode(treeNode);
                        return confirm("确认删除“" + treeNode.name + "”吗？");
                    }
                },
                onRemove: function (event, treeId, treeNode) {
                    $.post('/daily/wordType/delete/' + treeNode.id, function (result) {
                        if (result.errcode !== 0) {
                            console.log(result.message);
                        } else {
                            console.error(result.message);
                        }
                    })
                },
                onRename: function (event, treeId, treeNode, isCancel) {
                    $.post(
                        '/daily/wordType/save',
                        {
                            id: treeNode.id,
                            typeName: treeNode.name,
                            parentId: treeNode.pId,
                            depth: treeNode.level
                        },
                        function (result) {
                            if (result.errcode === 0) {
                                var data = result.data;
                                var treeObj = $.fn.zTree.getZTreeObj("wordTypeTree");
                                var nodes = treeObj.getSelectedNodes();
                                if (nodes.length>0) {
                                    nodes[0].id = data.id;
                                    nodes[0].code = data.code;
                                    treeObj.updateNode(nodes[0]);
                                }
                            } else {
                                var treeObj = $.fn.zTree.getZTreeObj("wordTypeTree");
                                var nodes = treeObj.getSelectedNodes();
                                treeObj.removeNode(nodes[0]);
                            }
                        }
                    )
                }
            }
        };
        wordTypeTree = $.fn.zTree.init($("#wordTypeTree"), setting);


        $('#moduleAdd').on('click', function (e) {
            var selectedNodes = wordTypeTree.getSelectedNodes(),
                treeNode = selectedNodes[0];
            if (treeNode) {
                treeNode = wordTypeTree.addNodes(treeNode, {pId: treeNode.id, name: "new node"});
            } else {
                treeNode = wordTypeTree.addNodes(null, {pId: 0, name: "new node"});
            }
            if (treeNode) {
                wordTypeTree.editName(treeNode[0]);
            } else {
                alert("叶子节点被锁定，无法增加子节点");
            }
        });

        $("#addWord").on('click', function (e) {
            var treeObj = $.fn.zTree.getZTreeObj("wordTypeTree");
            var nodes = treeObj.getSelectedNodes();
            if (!nodes || nodes.length === 0){
                alert("请选择左侧左侧分类树")
            }else {
                var typeId = nodes[0].id;
                window.location.href='/daily/word/add?typeId=' + typeId;
            }
        })
    })

    function getWordDataGrid(typeCode) {
        $.getJSON("/daily/word/datagrid", {typeCode: typeCode}, function (result) {
            if (result.errcode === 0) {
                var wordList = result.data;
                var html = '';
                $.each(wordList, function (index, word) {
                    html += '<div style="border-bottom: 1px solid #4db7fc;margin-bottom:20px;padding-bottom: 3px;">\n' +
                        '                <input type="hidden" value="' + word.id + '"/>\n' +
                        '                <div>\n' +
                        '                    <a href="/daily/word/detail/' + word.id + '">\n' +
                        '                        <div class="pull-left"><span class="glyphicon glyphicon-list"\n' +
                        '                                                     style="margin-right: 5px;"></span>' + word.word_title + '\n' +
                        '                        </div>\n' +
                        '                    </a>\n' +
                        '                    <div class="pull-right">' + word.word_time + '</div>\n' +
                        '                    <div class="clearfix"></div>\n' +
                        '                </div>\n' +
                        '                <div style="font-size: 12px;margin: 8px 0;">\n' +
                        '                    <div class="pull-left">作者：<span>' + word.word_author + '</span></div>\n' +
                        '                    <div class="pull-right">\n' +
                        '                    <span style="margin-right:15px"><a style="text-decoration: none" class="person-edit"\n' +
                        '                                                       href="/daily/word/edit/' + word.id + '">编辑</a></span>\n' +
                        '                        <span><a class="person-delete" style="text-decoration: none"\n' +
                        '                                 href="/daily/word/delete/' + word.id + '">删除</a></span>\n' +
                        '                    </div>\n' +
                        '                    <div class="clearfix"></div>\n' +
                        '                </div>\n' +
                        '            </div>';
                })
                $("#wordList").html(html);
            }
        })
    }




</script>
</html>